package cn.ibobo.lease.auth.service.impl;

import cn.ibobo.lease.auth.dao.AdminLoginLogMapper;
import cn.ibobo.lease.auth.dao.AdminMapper;
import cn.ibobo.lease.auth.service.AdminService;
import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.common.dto.AdminDTO;
import cn.ibobo.lease.common.dto.AdminLoginLogDTO;
import cn.ibobo.lease.common.util.DateUtil;
import cn.ibobo.lease.common.util.IpUtil;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;


/**
 * 管理员表业务处理接口
 * @Author lililiang
 * @Date 2018-12-09 18:17:26
 */
@Service
public class AdminServiceImpl extends BaseServiceImpl<AdminDTO, String> implements AdminService {

    private static final Log LOGGER = LogFactory.getLog(AdminServiceImpl.class);

    @Autowired
    private AdminMapper mapper;
    @Autowired
    private AdminLoginLogMapper logMapper;

    @Override
    protected BaseMapper<AdminDTO, String> getMapper() {
        return mapper;
    }

    /**
     * @Description 管理员登陆
     * @Author lililiang
     * @Date 2018/12/13 19:12
     * @Param [entityDTO]
     * @Return cn.ibobo.lease.common.dto.AdminDTO
     */
    @Override
    @Transactional
    public AdminDTO login(AdminDTO entityDTO, HttpServletRequest req) {
        //1. 获取登陆信息
        AdminDTO adminDTO = mapper.queryForLogin(entityDTO);
        if(adminDTO == null){
            return null;
        }else{
            //2. 登陆成功后保存登陆日志
            AdminLoginLogDTO logDTO = new AdminLoginLogDTO();
            logDTO.setAid(adminDTO.getAid());
            logDTO.setLoginip(IpUtil.getIpAddr(req));
            logDTO.setLogintm(DateUtil.getNow());
            logDTO.setId(UuidUtil.getUuid());
            logMapper.add(logDTO);
            return adminDTO;
        }


    }

}
